package top.timestreet.xmsk.w23txt.processor;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import top.timestreet.xmsk.base.pojo.SearchPage;
import top.timestreet.xmsk.base.processor.BaseSite;
import top.timestreet.xmsk.util.ChapterMeta;
import top.timestreet.xmsk.util.DESUtil;
import top.timestreet.xmsk.util.URLConvertUtil;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

/**
* @author TateLiao
* @data 2019年6月2日
* 天籁小说网(https://www.23txt.com/)搜索页面抓取规则
* egPage: https://www.23txt.com/search.php?keyword=大王饶命
*/
@Component
public class W23txtSearchPageProcessor extends BaseSite implements PageProcessor {

	@Override
	public void process(Page page) {
		List<SearchPage> searchPages = new ArrayList<>();
		List<Selectable> nodes = page.getHtml().$(".result-game-item").nodes();
		for (int i = 0; i < nodes.size(); i++) {
			Selectable selectable = nodes.get(i);
			SearchPage searchPage = new SearchPage();
			searchPage.setName(selectable.$(".result-game-item-title-link span", "text").toString());
			searchPage.setNameURL(URLConvertUtil.uRLConvert(DESUtil.encrypt(selectable.$(".result-game-item-title-link span", "href").toString())));
			searchPage.setLastChapter(selectable.$(".result-game-item-info-tag:nth-child(4) .result-game-item-info-tag-item", "text").toString());
			searchPage.setLastChapterUrl(URLConvertUtil.uRLConvert(DESUtil.encrypt(selectable.$(".result-game-item-info-tag:nth-child(4) .result-game-item-info-tag-item", "href").toString())));
			searchPage.setAuthor(selectable.$(".result-game-item-info-tag:nth-child(1) > span:nth-child(2)", "text").toString());
			searchPage.setLastUpdateTime(
					ChapterMeta.formatSearchPageLastUpdateTime(
							selectable.$(".result-game-item-info-tag:nth-child(3) .result-game-item-info-tag-title:nth-child(2)", "text").toString()));
			searchPages.add(searchPage);
			page.putField("list", searchPages);
		}
	}

	@Override
	public Site getSite() {
		return site;
	}

}
